#region

using System;
using System.Globalization;
using System.Runtime.InteropServices;
using FluentRibbon;
using FluentRibbon.Definitions;
using FluentRibbon.Definitions.Controls;
using FluentRibbon.Libraries;
using Microsoft.SharePoint;
using SpWiki.Lib.Extensions;
using SpWiki.Lib.Repository;

#endregion

namespace SpWiki.Features.Features
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("e7b6337c-d0b4-4cb7-8d24-130ed8c394dc")]
    public class FeaturesEventReceiver : SPFeatureReceiver
    {
        private Guid ReceiverGuid
        {
            get { return new Guid("600B36AE-C1AF-4F42-A5B7-5C2A2C239C8D"); }
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPWeb web = properties.Feature.Parent as SPWeb;

            var button1 = ControlLibrary.CreateRandomButton();
            var button2 = ControlLibrary.CreateRandomButton();
            var button3 = ControlLibrary.CreateRandomButton();

            var group = new GroupDefinition()
            {
                Id = "MyGroup",
                Title = "Custom controls",
                Template = GroupTemplateLibrary.SimpleTemplate,
                Controls = new ControlDefinition[]
            {
                button2,
                button3
            }
            };

            var ribbonCustomAction = new RibbonCustomAction();

            ribbonCustomAction.AddControlGroup(group, SPRibbonIds.Library.Id, 25);

            //// We need assign TemplateAlias manually to buttons, if they will be deployed separately
            //button1.TemplateAlias = "o1";
            //ribbonCustomAction.AddControl(button1, SPRibbonIds.Document.Groups.New.Id, 1);

            if (web != null)
            {
                var customActions = web.UserCustomActions;
                var templateId = web.GetListTemplateIdByInternalName("SpWikiDocumentLibraryDefinition");
                ribbonCustomAction.Provision(ReceiverGuid, customActions, templateId,
                                             ListForms.ListView, null);
            }

            #region Commented methods

            // Uncomment the method below to handle the event raised after a feature has been activated.

            // Uncomment the method below to handle the event raised after a feature has been installed.

            //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
            //{
            //}


            // Uncomment the method below to handle the event raised before a feature is uninstalled.

            //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
            //{
            //}

            // Uncomment the method below to handle the event raised when a feature is upgrading.

            //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
            //{
            //}

            #endregion Commented methods
        }

        public GroupDefinition GetGroupDefinition()
        {
            return new
                GroupDefinition
                       {
                           Id = SpWiki.Layouts.SpWiki.Forms.Ribbon.GroupId,
                           Title = SpWiki.Layouts.SpWiki.Forms.Ribbon.GroupTitle,
                           Template = GroupTemplateLibrary.SimpleTemplate,
                           Controls = new
                               ControlDefinition[]
                                          {
                                              new
                                                  ButtonDefinition
                                                  {
                                                      Id = SpWiki.Layouts.SpWiki.Forms.Ribbon.ButtonViewId,
                                                      Title = SpWiki.Layouts.SpWiki.Forms.Ribbon.ButtonViewTitle,
                                                      CommandJavaScript =
                                                          "NavigateToUrl('aaa');",
                                                      Image =
                                                          ImageLibrary.GetStandardImage(2, 14)
                                                  },
                                              new
                                                  ButtonDefinition
                                                  {
                                                      Id = SpWiki.Layouts.SpWiki.Forms.Ribbon.ButtonEditId,
                                                      Title = SpWiki.Layouts.SpWiki.Forms.Ribbon.ButtonEditTitle,
                                                      CommandJavaScript =
                                                          "NavigateToUrl('bbb');",
                                                      Image =
                                                          ImageLibrary.GetStandardImage(3, 5),
                                                  }
                                          }
                       };
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            var web = properties.Feature.Parent as SPWeb;
            RibbonCustomAction.RemoveAllCustomizations(web, ReceiverGuid);
         }
    }
}